package com.lzt.flow.component;

import com.alibaba.fastjson.JSONObject;
import com.lzt.flow.entity.DataExtractionForm;
import com.lzt.flow.entity.Node;
import com.lzt.flow.slot.FlowContext;
import com.yomahub.liteflow.core.NodeComponent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 开始节点
 *
 * @author lizhitao
 */
@Slf4j
@Component("startNode")
public class StartComponent extends NodeComponent {

    @Override
    public void process() throws Exception {
        log.info("开始节点");
        DataExtractionForm cmpData = JSONObject.parseObject(this.getCmpData(String.class), DataExtractionForm.class);
        FlowContext context = this.getContextBean(FlowContext.class);

        Node node = context.getCmpNode(cmpData.getNodeId());
        Map<String, List<Map<String, String>>> contextData = context.getData();
        if (node.getPreNode() == null) {
            contextData.put(node.getNodeName(), new ArrayList<>());
            return;
        }
        List<Map<String, String>> preNodeData = contextData.get(node.getPreNode().getNodeName());
        if (preNodeData == null) {
            contextData.put(node.getNodeName(), new ArrayList<>());
        } else {
            contextData.put(node.getNodeName(), preNodeData);
        }
    }
}
